phpcms v9外链文章获取点击量

默认phpcms v9的外链文章因为直接跳转(不进入详情页模板),导致外链文章的点击量一直是0,而我们有时需要按点击量排行或者排序的时候,就有问题了。

这里我们借助系统自带的点击量接口api来获取:
一、前台JS部分添加:

//点击外链执行文章点击量增加
$(".out_link").click(function(){
    $.get('api.php?op=count', {'id':$(this).attr("dataid"),'modelid':$(this).attr("mid")}, function (data) {
        console.log("数据: " + data);
  });
});

二、前台模板a链接标签中添加{if $r["islink"]} class="out_link" mid="{$CATEGORYS[$r['catid']]['modelid']}" dataid="{$r["id"]}"{/if}

{pc:content action="lists" catid="$catid" num="20" order="listorder DESC,inputtime DESC" page="$page"}
    
    {loop $data $r}
  • {if $r['thumb']}
    {$r['title']}
    {/if}

    {$r['title']}

    {$r['description']}
  • {/loop}
{$pages}
{/pc}

三、给推荐位中的a链接标签也添加{if $r["islink"]} class="out_link" mid="{$CATEGORYS[$r['catid']]['modelid']}" dataid="{$r["id"]}"{/if}
注:由于推荐位中无法判断链接是否是外链,所以这里我们改造下系统的推荐位方法函数(基于之前《phpcms v9未审核推荐信息出现在推荐列表中【BUG修复】》对该方法函数的改造),如下所示增加$rlink = $this->db->get_one(array('id'=>$info['id']), '`islink`');
$array[$key]['islink'] = $rlink['islink'];

文件位置:phpcms/modules/content/classes/content_tag.class.php

/**
	 * 推荐位
	 * WY EDIT AT 2024-01-09 已修复未审核信息出现在推荐列表中的问题
	 * @param $data
	 */
	public function position($data) {
		$sql = '';
		$array = array();
		$posid = intval($data['posid']);
$position = $this->position->get_one(array('posid'=>$posid));
$this->db->set_model($position['modelid']);
$this->tablename = $this->db->table_name;
$result = $this->db->select("status=99 AND posids=1", '*', '', '', '');
foreach ($result as $r) {
	$ids_array[] = $r['id'];
}
$ids = implode(',', $ids_array);
if($ids) {
	$sql = "id IN ($ids) AND ";
} else {
	$sql = '1<>1 AND';
}
		$order = $data['order'];
		$thumb = (empty($data['thumb']) || intval($data['thumb']) == 0) ? 0 : 1;
		$siteid = $GLOBALS['siteid'] ? intval($GLOBALS['siteid']) : 1;
		$catid = (empty($data['catid']) || $data['catid'] == 0) ? '' : intval($data['catid']);
		if($catid) {
			$siteids = getcache('category_content','commons');
			if(!$siteids[$catid]) return false;
			$siteid = $siteids[$catid];
			$this->category = getcache('category_content_'.$siteid,'commons');
		}
		if($catid && $this->category[$catid]['child']) {
			$catids_str = $this->category[$catid]['arrchildid'];
			$pos = strpos($catids_str,',')+1;
			$catids_str = substr($catids_str, $pos);
			$sql .= "`catid` IN ($catids_str) AND ";
		}  elseif($catid && !$this->category[$catid]['child']) {
			$sql .= "`catid` = '$catid' AND ";
		}
		if($thumb) $sql .= "`thumb` = '1' AND ";
		if(isset($data['where'])) $sql .= $data['where'].' AND ';
		if(isset($data['expiration']) && $data['expiration']==1) $sql .= '(`expiration` >= \''.SYS_TIME.'\' OR `expiration` = \'0\' ) AND ';
		$sql .= "`posid` = '$posid' AND `siteid` = '".$siteid."'";
		$pos_arr = $this->position_data->select($sql, '*', $data['limit'],$order);
		if(!empty($pos_arr)) {
			foreach ($pos_arr as $info) {
				$key = $info['catid'].'-'.$info['id'];
				$array[$key] = string2array($info['data']);
				$array[$key]['url'] = go($info['catid'],$info['id']);
				$rlink = $this->db->get_one(array('id'=>$info['id']), '`islink`');//给推荐位添加是否外链虚拟字段
				$array[$key]['islink'] = $rlink['islink'];
				$array[$key]['id'] = $info['id'];
				$array[$key]['catid'] = $info['catid'];
				$array[$key]['listorder'] = $info['listorder'];
			}
		}
		return $array;
	}

你可能感兴趣的:(PHPCMS,前端,php,javascript)