我的自定义元数据显示所有帖子类型或页面

时间:2016-08-20 05:56:31

标签: wordpress

我创建了一个自定义帖子类型'产品',还添加了一些元框。当我添加页面,帖子,菜单和任何帖子类型时,我的元框都显示在页面顶部的每个位置。我只想显示产品发布类型。

代码在这里。

  

帖子类型是产品

java.*

请检查我错在哪里。 请帮帮我。

4 个答案:

答案 0 :(得分:1)

试试这个:

将动作'init'更改为'add_meta_boxes'。

以下更改:

add_action( 'admin_init', 'marbel_product_meta_fields' );

替换为:

add_action( 'add_meta_boxes', 'marbel_product_meta_fields' );

答案 1 :(得分:1)

  

为了向所有帖子类型显示Meta框,您可以使用foreach循环通过循环结构迭代帖子类型。

试试这样,它会对你有用。

public class ForecastFragment extends Fragment {

private ArrayAdapter<String> mForecastAdapter;

public ForecastFragment() {
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Add this line in order for this fragment to handle menu events.
    setHasOptionsMenu(true);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.forecastfragment, menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_refresh) {
        updateWeather();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {


    // The ArrayAdapter will take data from a source and
    // use it to populate the ListView it's attached to.
    mForecastAdapter =
            new ArrayAdapter<String>(
                    getActivity(),// The current context (this activity)
                    R.layout.list_item_forecast,// The name of the layout ID.
                    R.id.tv_list_item_forecast, new ArrayList<String>());  // The ID of the textview to populate.

    // Log.e("weekForecast", "forecastArray: " + forecastArray + "/n" + weekForecast);

    View rootView = inflater.inflate(R.layout.fragment_main, container, false);

    ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast);
    listView.setAdapter(mForecastAdapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            String forecast = mForecastAdapter.getItem(position);
            Intent intent = new Intent(getActivity(), DetailActivity.class)
                    .putExtra(Intent.EXTRA_TEXT, forecast);
            startActivity(intent);
        }
    });
    return rootView;
}

private void updateWeather() {
   // FetchWeatherTask weatherTask = new FetchWeatherTask();
    FetchWeatherTask weatherTask = new FetchWeatherTask(getActivity(), mForecastAdapter);
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
    String location = prefs.getString(getString(R.string.pref_location_key),
            getString(R.string.pref_location_default));
    weatherTask.execute(location);
}

@Override
public void onStart() {
    super.onStart();
    updateWeather();
}

答案 2 :(得分:1)

删除此挂钩add_action( 'save_post', 'display_product_meta_box')这肯定有效!

答案 3 :(得分:0)

有一种解决方案,几乎可以在任何地方使用。首先,您需要使函数能够在WordPress中的任意位置查找帖子类型:

function marbel_get_post_type ($find = false) {
        global $post, $parent_file, $typenow, $current_screen, $pagenow;
        
        $post_type = NULL;
        
        if($post && (property_exists($post, 'post_type') || method_exists($post, 'post_type')))
            $post_type = $post->post_type;

        if(empty($post_type) && !empty($current_screen) && (property_exists($current_screen, 'post_type') || method_exists($current_screen, 'post_type')) && !empty($current_screen->post_type))
            $post_type = $current_screen->post_type;

        if(empty($post_type) && !empty($typenow))
            $post_type = $typenow;

        if(empty($post_type) && function_exists('get_current_screen'))
            $post_type = get_current_screen();

        if(empty($post_type) && isset($_REQUEST['post']) && !empty($_REQUEST['post']) && function_exists('get_post_type') && $get_post_type = get_post_type((int)$_REQUEST['post']))
            $post_type = $get_post_type;

        if(empty($post_type) && isset($_REQUEST['post_type']) && !empty($_REQUEST['post_type']))
            $post_type = sanitize_key($_REQUEST['post_type']);

        if(empty($post_type) && in_array($pagenow, array('edit.php', 'post-new.php')))
            $post_type = 'post';

        if(is_array($find))
        {
            return in_array($post_type, $find, true);
        }
        else if(is_string($find))
        {
            return ($post_type === $find);
        }
        
        $post_type = apply_filters( 'btnr_get_post_type', $post_type);
        
        return $post_type;
    }

然后,您可以执行以下操作:

function marbel_product_meta_fields() 
{
    if( marbel_get_post_type('product') ) return;
    /* 
        You can now add metaboxes here 
    */
}

...或类似的

function marbel_product_meta_fields() 
{
    if( marbel_get_post_type() == 'product' ) return;
    /* 
        You can now add metaboxes here 
    */
}

我提供给您的功能是我的艺术水平,您可以在任何地方将其用于任何想要的工作。

相关问题