2014-12-16 3 views
2

에 사용됩니다. $ i 변수는 함수가 호출 될 때마다 증가합니다. 기사 다음 기사 페이지에 카운터 리셋보다, 한 페이지 이상이있는 경우에워드 프레스 단축 코드 카운트 수는 나는 다음과 같은 워드 프레스 단축 코드 기능을 한 후

[shortcode][shortcode][shortcode][shortcode] 

그래서 출력은

[1][2][3][4] 

하지만

예상대로. 그래서 위해 :

어쨌든
[1][2][3][4] 
<!--nextpage--> 
[5][6][7][8] 

이를 변경하려면 :

[shortcode][shortcode][shortcode][shortcode] 
<!--nextpage--> 
[shortcode][shortcode][shortcode][shortcode] 

출력은

[1][2][3][4] 
<!--nextpage--> 
[1][2][3][4] 

대신의 원하는 출력? 덕분에

답변

1

그래서 조금 연구보다 사전 처리 the_content와 diggy의 아이디어 @와 단축 코드가 렌더링되기 전에 shorcodes에 $의 ATTS를 추가 한 후, 나는이 함께했다 :

function my_shortcode_content($content) { 
//check to see if the post has your shortcode, skip do nothing if not found 
if(has_shortcode($content, 'shortcode')): 

    //replace any shortcodes to the basic form ([shortcode 1] back to [shortcode]) 
    //usefull when the post is edited and more shortcodes are added 
    $content = preg_replace('/shortcode .]/', 'shortcode]', $content); 

    //loop the content and replace the basic shortcodes with the incremented value 
    $content = preg_replace_callback('/shortocode]/', 'rep_count', $content); 

endif; 

return $content; 
} 
add_filter('content_save_pre' , 'my_shortcode_content' , 10, 1); 

function rep_count($matches) { 
static $i = 1; 
return 'shortcode ' . $i++ .']'; 
} 
0

모든 페이지를로드 할 때마다 함수가 재정의 되니 항상 static $i=1;이 실행됩니다.

아마도 $_SESSION 변수를 사용하고 싶을 것입니다. 그러나 어딘가에 init 콜백을 잊지 마세요. 세션을 설정

function wp_shortcode() { 
    if (empty($_SESSION["myCounter"])) { 
     $_SESSION["myCounter"] = 1; 
    } 
    $return = '<a href="#f' . $_SESSION["myCounter"] . '" name="r' 
     . $_SESSION["myCounter"] . '">[' 
     . $_SESSION["myCounter"] . ']</a>'; 
    $_SESSION["myCounter"]++; 
    return $return; 
} 
add_shortcode('shortcode', 'wp_shortcode'); 

functions.php

add_action('init', 'my_init'); 
function my_init() { 
    if (!session_id()) { 
     session_start(); 
    } 
    //Other init here 
} 
+0

음이 작동을하지만, 페이지의 모든 갱신은 세션 종료하지 않고, 그래서 카운터 증가합니다 사용자가 떠날 때 이 문서는 작동하지 않습니다. 이 솔루션을 사용하는 유일한 방법은 게시물을 게시하는 것입니다. 따라서 기사를 읽기 시작하면 세션이 시작되고 다른 기사로 이동하면 세션이 끝납니다. – CiprianD

0

니스 질문에 넣어. 아래의 가능한 해결책은 정확하지는 않지만 각 페이지에 고정 된 수의 단축 코드가 있어야하기 때문입니다 (예 : 4 :

add_shortcode('shortcode', 'wp_shortcode'); 
function wp_shortcode() { 
    global $page; 
    $spp = 4; 
    static $i = 1; 
    $ii = $i + (($page - 1) * $spp); 
    $return = '<a href="#f'.$ii.'" name="r'.$ii.'">['.$ii.']</a>'; 
    $i++; 
    return $return; 
} 
+0

아쉽게도 기사/페이지마다 단축 코드의 수가 다릅니다 – CiprianD

+0

다른 변수로 정적 변수를 초기화 할 수 있습니까? 그래서,이 [shortcode 5]와 같은 shortcode에 속성을 추가하면 함수에서 정적 $ i = $ atts [0]이 기본적으로 정적 i = 5 일 수 있습니까? 시도했지만 오류가 발생하여 조정해야 할 수도 있습니다. – CiprianD

+1

PHP doc : "다른 PHP 정적 변수와 마찬가지로 정적 속성은 리터럴 또는 상수를 사용하여 초기화해야하며 표현식은 사용할 수 없습니다."이런 식으로 뭔가 일을 할 수 있지만 페이지에서 첫 번째 shortcode attr을보고 전달합니다 :'if (! SHRC ', $ atts [0]); = SH_C;'하지만 att로 숫자를 추가하기 시작하면 프로그래밍 방식으로 증가 시키면 의미가 없어지 겠지요? the_content를 사전 처리하고 동적으로 att를 추가하는 것이 더 좋은 방법일까요? 즉, 단축 코드가 처리되고 렌더링되기 전에 말입니다. – diggy

0

하는 경우 당신은 당신이 사용할 수있는 페이지/게시물에 총 태그 계산해야합니다 카운터가 올라가고 유지로,

function count_shortcodes($content, $tag) { 
    if (false === strpos($content, '[')) { 
     return 0; 
    } 

    if (shortcode_exists($tag)) { 
     preg_match_all('/' . get_shortcode_regex() . '/', $content, $matches, PREG_SET_ORDER); 
     if (empty($matches)) 
      return 0; 

     $count = 0; 
     foreach ($matches as $shortcode) { 
      if ($tag === $shortcode[2]) { 
       $count++; 
      } elseif (! empty($shortcode[5]) && has_shortcode($shortcode[5], $tag)) { 
       $count++; 
      } 
     } 

     return $count; 
    } 
    return 0; 
} 
관련 문제