كيف احصل على the_permalink في هذا الكود

استخدم هذا الكود للحصول على ارشيف الموقع بالاشهر

<?php
$all_posts = get_posts(array(
 'posts_per_page' => -1, // to show all posts
 'post_type' => 'news'

));

$ordered_posts = array();

foreach ($all_posts as $single) {

 $year = mysql2date('Y', $single->post_date);
 $month = mysql2date('F', $single->post_date);


 $ordered_posts[$year][$month][] = $single;


}

foreach ($ordered_posts as $year => $months) { ?>

<?php foreach ($months as $month => $posts ) { // iterates the moths ?>
   


  <a href=" link here ">
				<div class="arch-year-month">
				 
				<div class="arch-month"><?php echo $month ?></div><div class="arch-year"> <?php echo $year ?></div></div></a>
				
				
				
   <?php } 
   ?>
  <?php
} 

?>

لكن عند اضافة

<?php the_permalink(); ?>

لا يحصل على الرابط

يرجى الدخول لحسابك أو تسجيل حساب لتستطيع إضافة تعليق
حساب جديد دخول

التعليقات

@waelaljamal نعم وجدت الحل بطريقة أخرى عبر stackoverflow

عبر تمرير قيم الشهر عدديا وانشاء رابط

<?php
$all_posts = get_posts(array(
 'posts_per_page' => -1, 
 'post_type' => 'news'

));
$myMonth = array();
foreach ($all_posts as $single) {
 $month = mysql2date('F', $single->post_date);
 $month_number = mysql2date('m', $single->post_date);
 $myMonth[$month] = $month_number;
}
$ordered_posts = array();
foreach ($all_posts as $single) {
 $year = mysql2date('Y', $single->post_date);
 $month = mysql2date('F', $single->post_date);
 $month_number = mysql2date('m', $single->post_date);

 $ordered_posts[$year][$month][] = $single;
  
}

foreach ($ordered_posts as $year => $months) {
foreach ($months as $month => $posts ) { 
?>

<?php 

 ?>
  <a href="/news-date/<?php echo $year ?>/<?php echo $myMonth[$month]; ?>">
				<div class="arch-year-month">				 
				<div class="arch-month"><?php echo $month ?></div>
<div class="arch-year"> <?php echo $year ?></div>
				</div>
				</a>
				
   <?php
   } 
   
} 
?>

جرب بهذه الطريقة لأنك في طريقتك لا تطبعه

<a href="<?php the_permalink(); ?>">permalink</a>

بهذه الطريقة يحصل على رابط الصفحة ، لانني استخدمت الكود في صفحة مخصصة 'Template Name'

@waelaljamal بهذه الطريقة يحصل على رابط الصفحة ، لانني استخدمت الكود في صفحة مخصصة 'Template Name'

بما أن المتغيّر single هو الذي يحوي على عنصر المنشور post، يمكنك استخدام التابع get_permalink وتمرير معرّف المنشور ID كالتالي:

<?php echo get_permalink( $single->ID ); ?>

يمكنك الاطلاع على مزيد من التفاصيل حول permalink من توثيق وردبريس.

بهذه لطريقة يحصل على رابط المقال ، ما اريد الحصول عليه هو رابط الارشيف

مثل

../2022/05

هل وجدت حل مناسب؟

يمكنك مشاركته ليستفيد الجميع، شكرا لك